# Copyright (c) 2025 Li Auto Inc. and its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# definitions.mk
#

OUT_DIR?= out/$(TARGET_PRODUCT)
OUTOBJ:=$(OUT_DIR)/objs
BINDIR:=$(OUT_DIR)/bin
USRDIR:=$(OUT_DIR)/usr

comma := ,
define get_dir_path
$(patsubst %/,%,$(subst \,/,$(dir $1)))
endef

define my-dir
$(strip										\
  $(eval LOCAL_MODULE_MAKEFILE := $$(lastword $$(MAKEFILE_LIST)))		\
  $(if $(filter $(BUILD_SYSTEM)/% $(OUT_DIR)/%,$(LOCAL_MODULE_MAKEFILE)),	\
    $(error my-dir must be called before including any other makefile.),	\
    $(patsubst %/,%,$(dir $(LOCAL_MODULE_MAKEFILE)))				\
   )										\
 )
endef

################################################################################
# find expected files in the given subdirs.
# $(1): the base dir, relative to the root of the source tree.
# $(2): the file name pattern to be passed to find as "-name".
# $(3): a list of subdirs of the base dir.
# Returns: a list of paths relative to the base dir.
#
# EX: $(call find-files-in-subdirs system/core, "*.c", init fsmgr test)
################################################################################
define find-files-in-subdirs
$(sort									\
  $(patsubst ./%,%,							\
    $(shell cd $(1); find -L $(3) -name $(2) -and -not -name ".*")))
endef


################################################################################
## Find all of the files under the named directories with the specified name.
## Meant to be used like:
##    SRC_FILES := $(call all-named-files-under,*.h,src tests)
################################################################################
define all-named-files-under
$(call find-files-in-subdirs,$(LOCAL_PATH),"$(1)",$(2))
endef

###########################################################
## Convert a list of short module names (e.g., "framework", "Browser")
## into the list of files that are built for those modules.
## NOTE: this won't return reliable results until after all
## sub-makefiles have been included.
## $(1): target list
###########################################################
define module-built-files
$(foreach module,$(1),$(ALL_MODULES.$(module).BUILT))
endef

define module-dependent-static-libraries
$(foreach module,$(1),$(ALL_MODULES.$(module).STATIC_LIB))
endef

define copy-file-to-target
@mkdir -p $(dir $@)
$(hide) rm -f $@
$(hide) cp "$<" "$@"
endef

# Define a rule to copy a file.  For use via $(eval).
# $(1): source file
# $(2): destination file
define copy-one-file
$(2): $(1)
	@echo "Copy: $$@"
	$$(copy-file-to-target)
endef

# Copies many files.
# $(1): The files to copy.  Each entry is a ':' separated src:dst pair
# $(2): An optional directory to prepend to the destination
# Evaluates to the list of the dst files (ie suitable for a dependency list)
define copy-many-files
$(foreach f, $(1), $(strip \
    $(eval _cmf_tuple := $(subst :, ,$(f))) \
    $(eval _cmf_src := $(word 1,$(_cmf_tuple))) \
    $(eval _cmf_dest := $(word 2,$(_cmf_tuple))) \
    $(if $(strip $(2)), \
      $(eval _cmf_dest := $(patsubst %/,%,$(strip $(2)))/$(patsubst /%,%,$(_cmf_dest)))) \
    $(if $(filter-out $(_cmf_src), $(_cmf_dest)), \
      $(eval $(call copy-one-file,$(_cmf_src),$(_cmf_dest)))) \
    $(_cmf_dest)))
endef

# Copies many headers to a directory.
# $(1): The source directories of headers.
# $(2): The destination directory.
# Evaluates to the list of the dst files (ie suitable for a dependency list)
define copy-headers-under
$(eval _chu_dirs:=$(patsubst %/,%,$(1)))
$(foreach _chu_dir,$(_chu_dirs),                                                   \
     $(eval _chu_hdr_lst:=$(patsubst $(_chu_dir)/%,%,                              \
                               $(call all-named-files-under,*.h,$(_chu_dir))))     \
     $(foreach f,$(_chu_hdr_lst),                                                  \
          $(eval $(call copy-one-file,$(LOCAL_PATH)/$(_chu_dir)/$(f), $(2)/$(f)))  \
          $(2)/$(f)                                                                \
     )                                                                             \
)
endef

define print-var
$(warning $(1) = $($1))
endef

ALL_MODULES:=

include $(BUILD_SYSTEM)/binary.mk

CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk

BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mk
BUILD_STATIC_LIBRARY:= $(BUILD_SYSTEM)/static_library.mk
BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mk
# BUILD_HEADER_LIBRARY:= $(BUILD_SYSTEM)/header_library.mk
BUILD_PREBUILT:= $(BUILD_SYSTEM)/prebuilt.mk

define all_executable_modules
$(foreach m, $(ALL_MODULES), $(if $(filter EXECUTABLES,$(ALL_MODULES.$(m).CLASS)), $m))
endef

define all_executable_modules_install
$(foreach m, $(ALL_MODULES), $(if $(filter EXECUTABLES,$(ALL_MODULES.$(m).CLASS)), $m-install))
endef

define all_sdk_modules_install
$(foreach m, $(ALL_MODULES), $(if $(filter SHARED_LIBRARIES,$(ALL_MODULES.$(m).CLASS)), $m-install))
endef

define get_module_outdir
$(foreach m, $1, $(OUTOBJ)/$m)
endef

define get_module_target
$(foreach m, $1, $(OUTOBJ)/$m/$m)
endef

include build/tools/TestProfile/Makefile

# vim:ft=make
#
